package com.bw.call.config;

import com.bw.domain.Register;
import com.bw.openfeign.DoctorOpenfeign;
import com.fasterxml.jackson.annotation.JsonInclude;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.core.NestedExceptionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author liu
 * @date 2025/03/02 14:04
 **/
@Configuration
public class AiFunctionCallConfig {

    @Autowired
    private DoctorOpenfeign doctorOpenfeign;

    private static final Logger logger = LoggerFactory.getLogger(AiFunctionCallConfig.class);
    public record DoctorList(String tag) {}
    public record CancelRegisterRequest(String doctorName,String patientName) {}

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public record Doctor(long doctorId, String doctorName, String doctorTitle, double doctorPrice, String symptomName) {
    }

    @Bean
    @Description("根据病状查询医生")
    public Function<DoctorList, List<Doctor>> getDoctors() {
        return request -> {
            try {
                List<com.bw.domain.Doctor> doctorList = doctorOpenfeign.weCanTraceTheSymptomsToTheDoctor(request.tag());
                return doctorList.stream().map(doctor -> {
                    return new Doctor(
                            doctor.getDoctorId(),
                            doctor.getDoctorName(),
                            doctor.getDoctorTitle(),
                            doctor.getDoctorPrice(),
                            doctor.getSymptomName()
                    );
                }).collect(Collectors.toList());
            }
            catch (Exception e) {
                logger.warn("Get Doctor: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
                return new ArrayList<Doctor>();
            }
        };
    }

    @Bean
    @Description("根据医生姓名挂号")
    public Function<CancelRegisterRequest, String> register(){
        return request -> {
            try {
                Register register = new Register();
                register.setDoctorName(request.doctorName());
                register.setPatientName(request.patientName());
                doctorOpenfeign.register(register);
                return "挂号成功";
            }
            catch (Exception e) {
                logger.warn("Register Doctor: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
                return "挂号失败";
            }
        };
    }
    @Bean
    @Description("根据医生姓名取消挂号")
    public Function<CancelRegisterRequest, String> cancelRegister(){
        return request -> {
            try {
                Register register = new Register();
                register.setDoctorName(request.doctorName());
                register.setPatientName(request.patientName());
                doctorOpenfeign.cancelRegister(register);
                return "退号成功";
            }
            catch (Exception e) {
                logger.warn("Cancel Register Doctor: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
                return "退号失败";
            }
        };
    }
    @Bean
    @Description("根据医生姓名查询挂号状态")
    public Function<CancelRegisterRequest, Integer> getRegisterData(){
        return request -> {
            try {
                Register register = new Register();
                register.setDoctorName(request.doctorName());
                register.setPatientName(request.patientName());
                return doctorOpenfeign.getRegisterData(register);
            }
            catch (Exception e) {
                logger.warn("Get Register Data Doctor: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
                return 10;
            }
        };
    }

}
